package com.qc.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qc.bus.dao.BusGoodsDao;
import com.qc.bus.dao.BusInportDao;
import com.qc.bus.dao.BusOutportDao;
import com.qc.bus.entity.BusGoods;
import com.qc.bus.entity.BusInport;
import com.qc.bus.entity.BusOutport;
import com.qc.bus.entity.BusProvider;
import com.qc.bus.service.BusGoodsService;
import com.qc.bus.service.BusOutportService;
import com.qc.bus.service.BusProviderService;
import com.qc.bus.vo.OutportVo;
import com.qc.sys.common.ActiveUser;
import com.qc.sys.common.DataGridView;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * (BusOutport)表服务实现类
 *
 * @author AltmanQccc
 * @since 2020-09-10 20:14:56
 */
@Service
public class BusOutportServiceImpl extends ServiceImpl<BusOutportDao, BusOutport> implements BusOutportService {

    @Autowired
    private BusOutportDao outportDao;

    @Autowired
    private BusInportDao inportDao;

    @Autowired
    private BusGoodsService goodsService;

    @Autowired
    private BusProviderService providerService;

    @Override
    public BusOutport saveOutport(BusOutport outport) {
        Integer inportId=outport.getInportid();
        BusInport inport=inportDao.selectById(inportId);
        outport.setGoodsid(inport.getGoodsid());
        outport.setNumber(outport.getNumber());
        outport.setPaytype(inport.getPaytype());
        ActiveUser activeUser= (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        outport.setOperateperson(activeUser.getUser().getName());
        outport.setOutporttime(new Date());
        outport.setOutportprice(inport.getInportprice());
        outport.setProviderid(inport.getProviderid());
        //保存退货信息
        this.outportDao.insert(outport);
        //减少库存
        BusGoods goods=this.goodsService.getById(inport.getGoodsid());
        goods.setNumber(goods.getNumber()-outport.getNumber());
        this.goodsService.UpdateGoods(goods);
        return outport;
    }

    @Override
    public DataGridView queryAllOutport(OutportVo outportVo) {
        IPage<BusOutport> page = new Page<>(outportVo.getPage(),outportVo.getLimit());
        QueryWrapper<BusOutport> qw = new QueryWrapper<>();
        qw.eq(outportVo.getGoodsid()!=null,"goodsid",outportVo.getGoodsid());
        qw.eq(outportVo.getProviderid()!=null,"providerid",outportVo.getProviderid());
        qw.ge(outportVo.getStartTime()!=null,"outporttime",outportVo.getStartTime());
        qw.lt(outportVo.getEndTime()!=null,"outporttime",outportVo.getEndTime());
        this.outportDao.selectPage(page,qw);
        List<BusOutport> records = page.getRecords();
        for (BusOutport outport : records) {
            if (outport.getGoodsid()!=null){
                BusGoods goods = this.goodsService.getById(outport.getGoodsid());
                outport.setGoodsname(goods.getGoodsname());
                outport.setSize(goods.getSize());
            }
            if (outport.getProviderid()!=null){
                BusProvider provider = this.providerService.getById(outport.getProviderid());
                outport.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridView(page.getTotal(),page.getRecords());
    }
}